const Router = require('koa-router')
const jwt = require('koa-jwt')
const router = new Router({ prefix: '/users' })
const {
  checkOwner,
  checkUserExist,
  find,
  findById,
  create,
  update,
  delete: del,
  login,
  listFollowing,
  listFollowers,
  follow,
  unfollow,
  followTopic,
  unfollowTopic,
  listFollowingTopics,
  followQuestion,
  unfollowQuestion,
  listFollowingQuestions,
  listQuestions,
  listLikingAnswers,
  likeAnswer,
  unlikeAnswer,
  listDislikingAnswers,
  dislikeAnswer,
  undislikeAnswer,
  listCollectingAnswers,
  collectAnswer,
  uncollectAnswer
} = require('../controllers/users')
const { checkTopicExist } = require('../controllers/topics')
const { checkQuestionExist } = require('../controllers/questions')
const { checkAnswerExist } = require('../controllers/answers')
const { SECRET } = require('../config')
// 自定义认证中间件
// const auth = async (ctx, next) => {
//   // 没有authorization则默认值为'',以防replace报错
//   const { authorization = '' } = ctx.request.header
//   const token = authorization.replace('Bearer ', '')
//   try {
//     const user = jsonwebtoken.verify(token, SECRET)
//     ctx.state.user = user
//   } catch (error) {
//     ctx.throw(401, error.message)
//   }
//   await next()
// }
// 认证中间件
const auth = jwt({ secret: SECRET })
// 获取用户列表
router.get('/', find)
// 获取指定用户
router.get('/:id', checkUserExist, findById)
// 注册用户
router.post('/', create)
// 修改指定用户
router.patch('/:id', auth, checkOwner, checkUserExist, update)
// 删除指定用户
router.delete('/:id', auth, checkOwner, checkUserExist, del)
// 用户登录
router.post('/login', login)
// 获取某个用户的关注人列表
router.get('/:id/following', checkUserExist, listFollowing)
// 获取某个用户的粉丝列表
router.get('/:id/followers', checkUserExist, listFollowers)
// 关注某人
router.put('/following/:id', auth, checkUserExist, follow)
// 取消关注某人
router.delete('/following/:id', auth, checkUserExist, unfollow)
// 获取某个用户的关注话题列表
router.get('/:id/followingTopics', checkUserExist, listFollowingTopics)
// 关注某个话题
router.put('/followingTopics/:id', auth, checkTopicExist, followTopic)
// 取消关注某个话题
router.delete('/followingTopics/:id', auth, checkTopicExist, unfollowTopic)
// 获取某个用户的关注问题列表
router.get('/:id/followingQuestions', checkUserExist, listFollowingQuestions)
// 关注某个问题
router.put('/followingQuestions/:id', auth, checkQuestionExist, followQuestion)
// 取消关注某个问题
router.delete(
  '/followingQuestions/:id',
  auth,
  checkQuestionExist,
  unfollowQuestion
)
// 获取某个用户的提问列表
router.get('/:id/questions', checkUserExist, listQuestions)
// 获取某个用户点赞的答案列表
router.get('/:id/likingAnswers', checkUserExist, listLikingAnswers)
// 点赞某个答案，互斥关系是点赞后取消点踩
router.put(
  '/likingAnswers/:id',
  auth,
  checkAnswerExist,
  likeAnswer,
  undislikeAnswer
)
// 取消点赞某个答案
router.delete('/likingAnswers/:id', auth, checkAnswerExist, unlikeAnswer)
// 获取某个用户点踩的答案列表
router.get('/:id/dislikingAnswers', checkUserExist, listDislikingAnswers)
// 点踩某个答案，互斥关系是点踩后取消点赞
router.put(
  '/dislikingAnswers/:id',
  auth,
  checkAnswerExist,
  dislikeAnswer,
  unlikeAnswer
)
// 取消点踩某个答案
router.delete('/dislikingAnswers/:id', auth, checkAnswerExist, undislikeAnswer)
// 获取某个用户收藏的答案列表
router.get('/:id/collectingAnswers', checkUserExist, listCollectingAnswers)
// 收藏某个答案
router.put('/collectingAnswers/:id', auth, checkAnswerExist, collectAnswer)
// 取消收藏某个答案
router.delete('/collectingAnswers/:id', auth, checkAnswerExist, uncollectAnswer)
module.exports = router
